package com.sxz.sbs.module.client;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.applibrary.component.ClearEditText;
import com.sxz.sbs.applibrary.dialog.input.ValueInputBoardDialog;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.adapter.StallTypeAdapter;
import com.sxz.sbs.module.client.bean.BuildingBean;
import com.sxz.sbs.module.client.bean.StallRequireBean;
import com.sxz.sbs.module.client.presenter.AddClientPresenter;
import com.sxz.sbs.module.client.view.AddClientView;
import com.sxz.sbs.module.staff.AllParkingActivity;
import com.sxz.sbs.widget.CommonStar;
import com.sxz.sbs.widget.CommonTitle;
import com.willy.ratingbar.BaseRatingBar;
import com.willy.ratingbar.ScaleRatingBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author hp
 * @date 2018/1/19
 *
 * 添加业主
 */

public class AddClientActivity extends BaseActivity<AddClientPresenter> implements AddClientView {

    @BindView(R.id.ed_username)
    ClearEditText edUsername;
    @BindView(R.id.ed_phone)
    ClearEditText edPhone;
    @BindView(R.id.ed_phone_2)
    ClearEditText edPhone2;
    @BindView(R.id.ed_phone_3)
    ClearEditText edPhone3;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R.id.ed_buildnum)
    TextView edBuildnum;
    @BindView(R.id.tv_add_client_xq)
    TextView edXq;
    @BindView(R.id.ed_housenumber)
    ClearEditText edHouseNumber;
    @BindView(R.id.ed_annualincome)
    ClearEditText edAnnualincome;
    @BindView(R.id.ed_family)
    ClearEditText edFamily;
    @BindView(R.id.et_schedule)
    EditText etSchedule;
    @BindView(R.id.ed_pricemin)
    TextView edPriceMin;
    @BindView(R.id.ed_pricemax)
    TextView edPriceMax;
    @BindView(R.id.recy_stalltype)
    RecyclerView recyStallType;
    @BindView(R.id.clientBasics_ratingBar)
    ScaleRatingBar clientBasicsRatingBar;
    @BindView(R.id.clientbasics_text)
    TextView clientBasicsText;
    @BindView(R.id.tv_amend)
    TextView tvAmend;
    @BindView(R.id.checkbox_intent)
    CheckBox checkBox;

    private CommonStar commonStar = new CommonStar();
    private List<StallRequireBean> list = new ArrayList<>();
    private StallTypeAdapter stallTypeAdapter;
    private float starNum = 0;
    private String maxPrice = "", minPrice = "";
    private int buidingId;
    private int parkingId;

    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("添加业主").build().show();
        recyStallType.setLayoutManager(new GridLayoutManager(this, 4));
        stallTypeAdapter = new StallTypeAdapter(list);
        recyStallType.setAdapter(stallTypeAdapter);
        mPresenter.getIntentLabel(HttpParamMap.getParamMap());
        clientBasicsRatingBar.setOnRatingChangeListener(new BaseRatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(BaseRatingBar ratingBar, float rating) {
                starNum = rating;
                commonStar.initStar((int) rating, clientBasicsText);
            }
        });
        recyStallType.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (list.get(position).getIsSelect() == 0) {
                    list.get(position).setIsSelect(1);
                } else {
                    list.get(position).setIsSelect(0);
                }
                stallTypeAdapter.notifyDataSetChanged();
            }
        });
    }

    @OnClick({R.id.tv_amend
            , R.id.ed_buildnum, R.id.ed_pricemin, R.id.ed_pricemax, R.id.tv_add_client_xq})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_add_client_xq:
                startActivityForResult(new Intent(this, AllParkingActivity.class)
                                .putExtra("from_tag", 5)
                        , 1002);
                break;
            case R.id.ed_buildnum:
                if (TextUtils.isEmpty(edXq.getText().toString())) {
                    ToastUtils.showShort("请先选择小区");
                    return;
                }
                startActivityForResult(new Intent(this, BuildingNumberActivity.class)
                                .putExtra("parking_id", parkingId)
                        , 1004);
                break;
            case R.id.ed_pricemin:
                ValueInputBoardDialog dialog2 = new ValueInputBoardDialog(this, minPrice, ValueInputBoardDialog.UnitEnum.SELL);
                dialog2.setOnDialogResultListener(new ValueInputBoardDialog.OnDialogResultListener() {
                    @Override
                    public void onResult(String value, String unit) {
                        minPrice = value;
                        edPriceMin.setText(value + unit);
                    }
                });
                dialog2.show();
                break;
            case R.id.ed_pricemax:
                ValueInputBoardDialog dialog3 = new ValueInputBoardDialog(this, maxPrice, ValueInputBoardDialog.UnitEnum.SELL);
                dialog3.setOnDialogResultListener(new ValueInputBoardDialog.OnDialogResultListener() {
                    @Override
                    public void onResult(String value, String unit) {
                        maxPrice = value;
                        edPriceMax.setText(value + unit);
                    }
                });
                dialog3.show();
                break;
            case R.id.tv_amend:
                addClient();
                break;
            default:
                break;
        }
    }

    /**
     * 添加
     */
    private void addClient() {
        if (TextUtils.isEmpty(edUsername.getInputValue())) {
            ToastUtils.showShort("业主名字不能为空");
            return;
        }

        if (TextUtils.isEmpty(edPhone.getInputValue()) && TextUtils.isEmpty(edPhone2.getInputValue())
                && TextUtils.isEmpty(edPhone3.getInputValue())) {
            ToastUtils.showShort("手机号不能为空");
            return;
        }
        String phone = "";
        if (!TextUtils.isEmpty(edPhone.getInputValue())) {
            if (!RegexUtils.isMobileExact(edPhone.getInputValue()) && !RegexUtils.isTel(edPhone.getInputValue())) {
                ToastUtils.showShort("联系人方式1有错误");
                return;
            }
            phone = phone + edPhone.getInputValue() + ",";
        }
        if (!TextUtils.isEmpty(edPhone2.getInputValue())) {
            if (!RegexUtils.isMobileExact(edPhone2.getInputValue()) && !RegexUtils.isTel(edPhone2.getInputValue())) {
                ToastUtils.showShort("联系人方式2有错误");
                return;
            }
            phone = phone + edPhone2.getInputValue() + ",";
        }
        if (!TextUtils.isEmpty(edPhone3.getInputValue())) {
            if (!RegexUtils.isMobileExact(edPhone3.getInputValue()) && !RegexUtils.isTel(edPhone3.getInputValue())) {
                ToastUtils.showShort("联系人方式3有错误");
                return;
            }
            phone = phone + edPhone3.getInputValue() + ",";
        }
        if (TextUtils.isEmpty(edXq.getText().toString())) {
            ToastUtils.showShort("请选择小区");
            return;
        }
        if (TextUtils.isEmpty(edBuildnum.getText().toString())) {
            ToastUtils.showShort("请选择具体楼号");
            return;
        }
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        map.put("a.Build_Id", buidingId);
        map.put("a.Parking_Id", parkingId);
        map.put("a.txt_UserName", edUsername.getInputValue());
        phone = phone.substring(0, phone.length() - 1);
        map.put("a.txt_Phone", phone);
        map.put("a.txt_Sex", radioGroup.getCheckedRadioButtonId() == R.id.man ? "男" : "女");
        map.put("a.addr_Address", edHouseNumber.getInputValue());
        map.put("a.txt_Income", edAnnualincome.getInputValue());
        map.put("a.txt_FamilyInfo", edFamily.getInputValue());
        map.put("a.val_PriceMin", minPrice);
        map.put("a.val_PriceMax", maxPrice);
        map.put("valStarLevel", (int) starNum);
        String lebal = "";
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getIsSelect() == 1) {
                lebal = lebal + list.get(i).getIntentWord() + "|";
            }
        }
        if (!TextUtils.isEmpty(lebal)) {
            lebal = lebal.substring(0, lebal.length() - 1);
        }
        map.put("a.txt_IntentLabel", lebal);
        map.put("b.txt_Content", etSchedule.getText().toString());
        map.put("isIntent", checkBox.isChecked() ? 2 : 1);
        mPresenter.getAddClient(map);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_addclient;
    }

    @Override
    protected AddClientPresenter createPresenter() {
        return new AddClientPresenter();
    }

    @Override
    public void onAddClientSuccess(String data) {
        finish();
    }

    @Override
    public void onGetStallRequireSuccess(List<StallRequireBean> data) {
        if (mIsLive && data != null) {
            list.clear();
            list.addAll(data);
            stallTypeAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        if (requestCode == 1002 && resultCode == 1006) {
            parkingId = data.getIntExtra("parking_id", 0);
            edXq.setText(data.getStringExtra("parking_name"));
        } else if (requestCode == 1004) {
            BuildingBean.ListBean bean = data.getParcelableExtra("building_result");
            if (bean != null) {
                buidingId = bean.getId();
                edBuildnum.setText(bean.getTxt_BuildNum());
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
